package com.tongzhan.service;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.tongzhan.base.BaseCriteria;
import com.tongzhan.base.BaseShareService;
import com.tongzhan.entity.MarkPrompt;
import com.tongzhan.entity.User;
import com.tongzhan.entity.party.PeopleOpinion;
import com.tongzhan.utils.DateSwitcher;
import com.tongzhan.utils.ThreadLocalUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author htmic
 * @date 2020/7/29
 */
@Service
public class OpinionService extends BaseShareService {

    /**
     * 获取列表
     *
     * @param year_and_month
     * @param page
     * @param size
     * @param userId
     * @return
     */
    public List getList(String year_and_month, Integer page, Integer size, String userId) {
        List<Map<String, Object>> list = new ArrayList<>();

        //处理时间查询
//        Date startDate = null;
//        Date endDate = null;
//        Date nowDate = new Date();
//        if (type == 1) {
//            //查询 一周
//            startDate = DateSwitcher.getWeekFirstDay(nowDate);
//            endDate = DateSwitcher.getWeekLastDay(nowDate);
//        } else if (type == 2) {
//            //查询 一月
//            startDate = DateSwitcher.getMonthFirstDay(nowDate);
//            endDate = DateSwitcher.getMonthLastDay(nowDate);
//        } else if (type == 2) {
//            //查询 一季
//            startDate = DateSwitcher.getSeasonFirstDay(nowDate);
//            endDate = DateSwitcher.getSeasonLastDay(nowDate);
//        } else if (type == 2) {
//            //查询 半年
//            startDate = DateSwitcher.getHalfYearFirstDay(nowDate);
//            endDate = DateSwitcher.getHalfYearLastDay(nowDate);
//        } else {
//            //查询 一年
//            startDate = DateSwitcher.getYearFirstDay(nowDate);
//            endDate = DateSwitcher.getYearLastDay(nowDate);
//        }

        //处理时间查询
        Date startDate = null;
        Date endDate = null;
        if (!StringUtil.isEmpty(year_and_month)){
            //查询年月 YYYY-MM
            Date date = DateSwitcher.string2Date(year_and_month, "yyyy-MM");
            startDate = DateSwitcher.getMonthFirstDay(date);
            endDate = DateSwitcher.getMonthLastDay(date);
        }

        //党派工商科 查看全部
        Page<PeopleOpinion> pageList1 = null;
        Criteria c1 = BaseCriteria.where("is_del").is(0).and("type").is(1);
        if(startDate!=null){
            c1.and("time").gte(startDate).lte(endDate);
        }
        Query qw1 = new Query().addCriteria(c1);
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
            qw1.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
            qw1.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
        }
        PageRequest of1 = PageRequest.of(page, size, Sort.Direction.DESC, "time");
        pageList1 = peopleOpinionRepository.findPage(qw1, of1);

        //党派只能看到属于自己的信息
        Page<PeopleOpinion> pageList2 = null;
        Criteria c2 = BaseCriteria.where("is_del").is(0).and("addUserId").is(userId).and("type").is(1);
        if(startDate!=null){
            c2.and("time").gte(startDate).lte(endDate);
        }
        Query qw2 = new Query(c2);
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
            qw2.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
            qw2.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
        }
        PageRequest of2 = PageRequest.of(page, size, Sort.Direction.DESC, "time");
        pageList2 = peopleOpinionRepository.findPage(qw2, of2);


        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        map1.put("gskList", pageList1.getContent());
        map1.put("total", pageList1.getTotalElements());
        map2.put("dpList", pageList2.getContent());
        map2.put("total", pageList2.getTotalElements());

        list.add(map1);
        list.add(map2);
        return list;
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    public PeopleOpinion getDetail(String id,String userId) {
        //标记角标已读
        markPromptService.read(id,userId);
        return peopleOpinionRepository.findByPrimaryKey(id);
    }

    /**
     * 添加
     * @param saveModel
     * @return
     */
    public long add(PeopleOpinion saveModel) {
        saveModel.setType(1);
        saveModel.setIs_del(0);
        saveModel.setAddTime(new Date());
        saveModel.setTime(new Date());
        User userById = findUserById(saveModel.getAddUserId());
        if(userById!=null){
            saveModel.setAddUserName(userById.getUserName());
        }
        saveModel.setStreetCode(ThreadLocalUtil.getThreadLocal().getStreetCode());
        saveModel.setVillageCode(ThreadLocalUtil.getThreadLocal().getVillageCode());
        long l = peopleOpinionRepository.insertSelective(saveModel);
        if(l>0){
            //添加角标
            markPromptService.add(new MarkPrompt(4, saveModel.getId()),"11");
            return  1;
        }
        return 0;
    }
}
